package pers.jphoebe.quartz.api.juhe.impl;

import okhttp3.*;
import org.springframework.stereotype.Component;
import pers.jphoebe.quartz.api.juhe.GoldApi;
import pers.jphoebe.quartz.api.juhe.JuheApi;
import pers.jphoebe.quartz.config.juhe.JuheParamConfig;
import pers.jphoebe.quartz.entity.juhe.GoldDataMap;
import pers.jphoebe.quartz.entity.juhe.JuheResponse;
import pers.jphoebe.quartz.entity.response.ErrorCode;
import pers.jphoebe.quartz.entity.response.ResponseData;
import pers.jphoebe.quartz.handler.JuheResponseHandler;
import pers.jphoebe.quartz.utils.*;

import java.util.ArrayList;

/**
 * Created by 蒋时华 on 2017/10/31.
 */
@Component
public class GoldApiImpl implements GoldApi {

    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public ResponseData<JuheResponse<ArrayList<GoldDataMap>>> getDataByShgold() {

        return new JuheResponseHandler<ResponseData<JuheResponse<ArrayList<GoldDataMap>>>>().handle(new JuheApi<ResponseData<JuheResponse<ArrayList<GoldDataMap>>>>() {
            @Override
            public ResponseData<JuheResponse<ArrayList<GoldDataMap>>> invokeJuheAPI() {
                OkHttpClient client = new OkHttpClient();
//                String credential = Credentials.basic(accessTokenRequest.getClientId(), accessTokenRequest.getClientSecret());
                RequestBody body = new FormBody.Builder()
                        .add("v", "1")
                        .add("key", JuheParamConfig.GOLD_APPKEY)
                        .build();
                Request request = new Request.Builder()
//                        .header("Authorization", credential)
                        .url(JuheParamConfig.GOLD_URL)
                        .post(body)
                        .build();

                try {
//                    Response response = client.newCall(request).execute();
//                    if(!response.isSuccessful()){
//                        return ResponseDataUtils.error(ErrorCode.ERROR_CODE);
//                    }
//                    String res = response.body().string();
                    String res = "{\"resultcode\":\"200\",\"reason\":\"SUCCESSED!\",\"result\":[{\"Au100g\":{\"variety\":\"Au100g\",\"latestpri\":\"273.58\",\"openpri\":\"272.75\",\"maxpri\":\"274.35\",\"minpri\":\"272.75\",\"limit\":\"0.19%\",\"yespri\":\"273.07\",\"totalvol\":\"214.00\",\"time\":\"2017-10-31 15:25:52\"},\"Au(T+N1)\":{\"variety\":\"Au(T+N1)\",\"latestpri\":\"278.75\",\"openpri\":\"277.80\",\"maxpri\":\"279.00\",\"minpri\":\"277.80\",\"limit\":\"0.31%\",\"yespri\":\"277.90\",\"totalvol\":\"229206.00\",\"time\":\"2017-10-31 15:30:24\"},\"Au(T+D)\":{\"variety\":\"Au(T+D)\",\"latestpri\":\"273.80\",\"openpri\":\"272.93\",\"maxpri\":\"273.95\",\"minpri\":\"272.93\",\"limit\":\"0.27%\",\"yespri\":\"273.05\",\"totalvol\":\"65092.00\",\"time\":\"2017-10-31 15:30:44\"},\"Au99.99\":{\"variety\":\"Au99.99\",\"latestpri\":\"273.90\",\"openpri\":\"273.75\",\"maxpri\":\"274.10\",\"minpri\":\"273.01\",\"limit\":\"0.36%\",\"yespri\":\"272.92\",\"totalvol\":\"112853.00\",\"time\":\"2017-10-31 15:30:39\"},\"Au99.95\":{\"variety\":\"Au99.95\",\"latestpri\":\"273.94\",\"openpri\":\"273.70\",\"maxpri\":\"273.94\",\"minpri\":\"273.65\",\"limit\":\"0.33%\",\"yespri\":\"273.05\",\"totalvol\":\"858.00\",\"time\":\"2017-10-31 15:30:35\"},\"Au50g\":{\"variety\":\"Au50g\",\"latestpri\":\"--\",\"openpri\":\"--\",\"maxpri\":\"--\",\"minpri\":\"--\",\"limit\":\"--\",\"yespri\":\"255.00\",\"totalvol\":\"--\",\"time\":\"2017-10-31 15:30:44\"},\"Ag99.99\":{\"variety\":\"Ag99.99\",\"latestpri\":\"3840.00\",\"openpri\":\"3840.00\",\"maxpri\":\"3840.00\",\"minpri\":\"3840.00\",\"limit\":\"0.00%\",\"yespri\":\"3840.00\",\"totalvol\":\"144.00\",\"time\":\"2017-10-30 20:00:00\"},\"Ag(T+D)\":{\"variety\":\"Ag(T+D)\",\"latestpri\":\"3814.00\",\"openpri\":\"3801.00\",\"maxpri\":\"3818.00\",\"minpri\":\"3801.00\",\"limit\":\"0.24%\",\"yespri\":\"3805.00\",\"totalvol\":\"1736700.00\",\"time\":\"2017-10-31 15:30:44\"},\"Au(T+N2)\":{\"variety\":\"Au(T+N2)\",\"latestpri\":\"274.70\",\"openpri\":\"274.40\",\"maxpri\":\"275.50\",\"minpri\":\"274.40\",\"limit\":\"0.16%\",\"yespri\":\"274.25\",\"totalvol\":\"224826.00\",\"time\":\"2017-10-31 15:30:24\"},\"Pt99.95\":{\"variety\":\"Pt99.95\",\"latestpri\":\"212.28\",\"openpri\":\"212.02\",\"maxpri\":\"212.28\",\"minpri\":\"211.76\",\"limit\":\"0.71%\",\"yespri\":\"210.79\",\"totalvol\":\"26.00\",\"time\":\"2017-10-31 15:25:40\"},\"AU995\":{\"variety\":\"AU995\",\"latestpri\":\"--\",\"openpri\":\"--\",\"maxpri\":\"--\",\"minpri\":\"--\",\"limit\":\"--\",\"yespri\":\"244.15\",\"totalvol\":\"--\",\"time\":\"2017-10-30 20:00:00\"},\"AU99.99\":{\"variety\":\"AU99.99\",\"latestpri\":\"273.90\",\"openpri\":\"273.75\",\"maxpri\":\"274.10\",\"minpri\":\"273.01\",\"limit\":\"0.36%\",\"yespri\":\"272.92\",\"totalvol\":\"1128534.00\",\"time\":\"2017-10-31 15:30:39\"},\"MAUTD\":{\"variety\":\"MAUTD\",\"latestpri\":\"273.89\",\"openpri\":\"273.05\",\"maxpri\":\"274.20\",\"minpri\":\"273.05\",\"limit\":\"0.26%\",\"yespri\":\"273.18\",\"totalvol\":\"90924.00\",\"time\":\"2017-10-31 15:30:44\"},\"IAU99.99\":{\"variety\":\"IAU99.99\",\"latestpri\":\"272.70\",\"openpri\":\"272.45\",\"maxpri\":\"272.70\",\"minpri\":\"272.00\",\"limit\":\"0.41%\",\"yespri\":\"271.60\",\"totalvol\":\"122998.00\",\"time\":\"2017-10-31 15:30:38\"},\"IAU100G\":{\"variety\":\"IAU100G\",\"latestpri\":\"--\",\"openpri\":\"--\",\"maxpri\":\"--\",\"minpri\":\"--\",\"limit\":\"--\",\"yespri\":\"275.00\",\"totalvol\":\"--\",\"time\":\"2017-10-31 11:06:23\"},\"IAU99.5\":{\"variety\":\"IAU99.5\",\"latestpri\":\"--\",\"openpri\":\"--\",\"maxpri\":\"--\",\"minpri\":\"--\",\"limit\":\"--\",\"yespri\":\"237.80\",\"totalvol\":\"--\",\"time\":\"2017-10-30 20:00:00\"}}],\"error_code\":0}";
                    JuheResponse<ArrayList<GoldDataMap>> juheResponse = FastJsonTools.toBeanForJuheResponsedataListGoldDataList(res);
                    return ResponseDataUtils.success(juheResponse);
                } catch (Exception e) {
                    logger.error(StackTraceUtils.getStackTraceStr(e));
                }

                return ResponseDataUtils.error(ErrorCode.ERROR_CODE);
            }
        });
    }
}
